package com.freemyleft.left.left_app.launcher;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.zapp.app.AccountManager;
import com.freemyleft.left.zapp.app.IUserChecker;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.ui.launcher.ILauncherListener;
import com.freemyleft.left.zapp.ui.launcher.LauncherHolderCreator;
import com.freemyleft.left.zapp.ui.launcher.OnLauncherFinshTag;
import com.freemyleft.left.zapp.ui.launcher.ScrollLauncherTag;
import com.freemyleft.left.zapp.util.LeftPreference;

import java.util.ArrayList;

/**
 * Created by FreeMyleft on 2017/12/8.
 */

public class LauncherScrollDelegate extends LeftDelegate implements OnItemClickListener {
    private ILauncherListener mLauncherListener=null;
    //轮播控件对应的泛型是数据类型 传入的是一个资源文件 所以是一个integer
    private ConvenientBanner<Integer> mIntegerConvenientBanner=null;
    private  static final ArrayList<Integer>INTEGERS=new ArrayList<>();
    private void initBanner(){
        INTEGERS.add(R.mipmap.wxxx );
        INTEGERS.add(R.mipmap.wxxx);
        INTEGERS.add(R.mipmap.wxxx);
        mIntegerConvenientBanner
                //设置图片的资源数据
                .setPages(new LauncherHolderCreator(),INTEGERS)
                //设置索引标识的资源图片
                .setPageIndicator(new int[]{R.drawable.dot_normal, R.drawable.dot_focus})
                //设置索引标识的位置 居中
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(this)
                //设置可以循环
                .setCanLoop(false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherListener){
            mLauncherListener= (ILauncherListener) activity;
        }
    }

    @Override
    public Object setLayout() {
        mIntegerConvenientBanner=new ConvenientBanner<>(getContext());
        return mIntegerConvenientBanner;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
   initBanner();
    }

    @Override
    public void onItemClick(int position) {
        //如果点击的是最后一个
        if (position==INTEGERS.size()-1){
            //是否第一次登录App
            LeftPreference.setAppFlag(ScrollLauncherTag.HAS_FIRST_LAUNCHER_APP.name(),true);
            // TODO: 2017/12/8  检查用户是否已经登录
            //检查用户是否登录了APp
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSigIn() {
                    if (mLauncherListener!=null){
                        mLauncherListener.onLauncherFinish(OnLauncherFinshTag.SIGNED);
                    }

                }

                @Override
                public void onNotSigIn() {
                    if (mLauncherListener!=null){
                        mLauncherListener.onLauncherFinish(OnLauncherFinshTag.NOT_SIGNED);
                    }

                }
            });
        }

    }
}
